#include "MainWindow.h"
#include "RenderWidget.h"
#include "Controller.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QWidget>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , renderWidget(new RenderWidget(this))
    , controller(new Controller(renderWidget, this))
{
    setupUi();
    setupConnections();
}

MainWindow::~MainWindow() {
    // QObject 父子机制会自动 delete
}

void MainWindow::setupUi() {
    // 中央容器
    QWidget *central = new QWidget(this);
    QHBoxLayout *hLayout = new QHBoxLayout(central);

    // 左侧控制组
    controlGroup_ = new QGroupBox(tr("控制面板"), central);
    controlGroup_->setMaximumWidth(220);
    QVBoxLayout *vLayout = new QVBoxLayout(controlGroup_);

    // 向量输入
    vLayout->addWidget(new QLabel(tr("向量 1 (X, Y):"), controlGroup_));
    leX1_ = new QLineEdit(controlGroup_);
    leY1_ = new QLineEdit(controlGroup_);
    vLayout->addWidget(leX1_);
    vLayout->addWidget(leY1_);
    vLayout->addSpacing(10);
    vLayout->addWidget(new QLabel(tr("向量 2 (X, Y):"), controlGroup_));
    leX2_ = new QLineEdit(controlGroup_);
    leY2_ = new QLineEdit(controlGroup_);
    vLayout->addWidget(leX2_);
    vLayout->addWidget(leY2_);
    btnAdd_ = new QPushButton(tr("添加向量"), controlGroup_);
    vLayout->addWidget(btnAdd_);
    vLayout->addSpacing(20);

    // 矩阵输入
    vLayout->addWidget(new QLabel(tr("矩阵 A11, A12:"), controlGroup_));
    leA11_ = new QLineEdit(controlGroup_);
    leA12_ = new QLineEdit(controlGroup_);
    vLayout->addWidget(leA11_);
    vLayout->addWidget(leA12_);
    vLayout->addSpacing(10);
    vLayout->addWidget(new QLabel(tr("矩阵 A21, A22:"), controlGroup_));
    leA21_ = new QLineEdit(controlGroup_);
    leA22_ = new QLineEdit(controlGroup_);
    vLayout->addWidget(leA21_);
    vLayout->addWidget(leA22_);
    btnTransform_ = new QPushButton(tr("矩阵变换"), controlGroup_);
    vLayout->addWidget(btnTransform_);
    vLayout->addStretch();
    controlGroup_->setLayout(vLayout);

    // 右侧渲染区
    renderWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 布局比例：控制面板 1，渲染区 3
    hLayout->addWidget(controlGroup_, 1);
    hLayout->addWidget(renderWidget, 3);

    central->setLayout(hLayout);
    setCentralWidget(central);
}

void MainWindow::setupConnections() {
    connect(btnAdd_, &QPushButton::clicked, this, &MainWindow::onAddClicked);
    connect(btnTransform_, &QPushButton::clicked, this, &MainWindow::onTransformClicked);
}

void MainWindow::onAddClicked() {
    // 从文本框读取数据并调用 Controller
    float x1 = leX1_->text().toFloat();
    float y1 = leY1_->text().toFloat();
    float x2 = leX2_->text().toFloat();
    float y2 = leY2_->text().toFloat();
    controller->onAddVectors(x1, y1, x2, y2);
}

void MainWindow::onTransformClicked() {
    // 从文本框读取数据并调用 Controller
    float a11 = leA11_->text().toFloat();
    float a12 = leA12_->text().toFloat();
    float a21 = leA21_->text().toFloat();
    float a22 = leA22_->text().toFloat();
    controller->onTransformMatrix(a11, a12, a21, a22);
}
